<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<div th:insert="/common.html"></div>
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <SCRIPT type="text/javascript">
    var setting = {
      data: {
        simpleData: {
          enable: true
        }
      },
      check: {
        enable: true,
        autoCheckTrigger: true
      },
      async: {
        enable: true,
        url: "/menu/getInfoZtree",
        autoParam: ["id", "name"]
      },
      view : {
        addHoverDom: function(treeId, treeNode){
          var aObj = $("#" + treeNode.tId + "_a");
          if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
          var s = '<span id="btnGroup'+treeNode.tId+'">';
          if ( treeNode.level == 0 ) {
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
          } else if ( treeNode.level == 1 ) {
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
            if (treeNode.children == undefined) {
              s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
            }
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
          } else if ( treeNode.level == 2 ) {
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
          }
          s += '</span>';
          aObj.after(s);
        },
        removeHoverDom: function(treeId, treeNode){
          $("#btnGroup"+treeNode.tId).remove();
        }
      }
    };

    $(document).ready(function(){
      $.fn.zTree.init($("#treeDemo"), setting);
    });

    //增加
    function addNode(id){
      BootstrapDialog.show({
        title: '提示',//标题
        message: $('<div></div>').load('/menu/toAdd'),//显示内容
        type: BootstrapDialog.TYPE_DEFAULT,//设置提示框类型
        size: BootstrapDialog.SIZE_WIDE,//设置提示框大小

        buttons: [{
          label: '增加',
          closeable: true,//按钮颜色
          action: function (dialog) {
            $.ajax({
              url:"/menu/add",
              dataType: "json",
              type:"post",
              data: $("#insert_form").serialize() + "&parentid="+ id,
              success:function (result){
                if (result){
                  dialog.close();
                  toastr.success("增加成功");
                  $.fn.zTree.init($("#treeDemo"), setting);
                }
              },
              error:function (){
                toastr.error("系统错误")
              }
            })
          }
        }, {
          label: '关闭',
          action: function (dialog) {
            dialog.close();
          }
        }]
      });
    }

    //修改
    function editNode(mid){
      BootstrapDialog.show({
        title: '提示',//标题
        message: $('<div></div>').load('/menu/toUpdate/' + mid),//显示内容
        type: BootstrapDialog.TYPE_DEFAULT,//设置提示框类型
        size: BootstrapDialog.SIZE_WIDE,//设置提示框大小

        buttons: [{
          label: '修改',
          closeable: true,//按钮颜色
          action: function (dialog) {
            $.ajax({
              url:"/menu/updateInfo",
              dataType: "json",
              type:"post",
              data: $("#update_form").serialize(),
              success:function (result){
                if (result){
                  dialog.close();
                  toastr.success("修改成功");
                  $.fn.zTree.init($("#treeDemo"), setting);
                }
              },
              error:function (){
                toastr.error("系统错误")
              }
            })
          }
        }, {
          label: '关闭',
          action: function (dialog) {
            dialog.close();
          }
        }]
      });
    }

    function deleteNode(id){
      if (confirm("确认删除")){
        $.ajax({
          url:"/menu/deleteInfo/" + id,
          type:"post",
          dataType:"json",
          success:function (result){
            if (result){
              toastr.success("删除成功");
              $.fn.zTree.init($("#treeDemo"), setting);
            }
          },
          error:function (){
            toastr.error("系统错误")
          }
        })
      }
    }

  </SCRIPT>
</head>
<body>
    <div class="zTreeDemoBackground left">
      <ul id="treeDemo" class="ztree"></ul>
    </div>
</body>
</html>